import { _decorator, Animation, AnimationClip, Component, instantiate, Node, Prefab, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('EnimyControl')
export class EnimyControl extends Component {
  @property(Prefab)
  arrow: Prefab = null
  direction: boolean = false
  isMove: boolean = false
  isAttack: boolean = true
  @property(Number)
  speed: number = 20
  animator: Animation = null
  animationClips: AnimationClip[] = []

  start() {
    this.animator = this.node.getComponent(Animation)
    this.animationClips.push(...this.animator.clips)
    console.log(this.animationClips);
    
    this.isMove = true
    this.animator.defaultClip = this.animationClips[1]
    this.animator.play()
    this.schedule(() => {
      this.isMove = false
      this.animator.defaultClip = this.animationClips[0]
      this.animator.play()
    }, 2,1)
  }

  update(deltaTime: number) {
    if (this.isMove) {
      this.move(deltaTime)
    }
  }
  shot() {
    this.isAttack = true
    const arrow = instantiate(this.arrow)
    const { x, y, z } = this.node.position
    const skewX = this.direction ? x + 40 : x - 40
    const skewY = y + 30
    arrow.setPosition(new Vec3(skewX, skewY, z))
    this.node.getParent().addChild(arrow)
  }
  move(dt: number) {
    this.isMove = true
    const { x, y, z } = this.node.position
    const skewX = this.direction ? x + this.speed * dt : x - this.speed * dt
    this.node.setPosition(new Vec3(skewX, y, z))
  }
}


